// 存储的状态信息
import { StoreOptions } from "vuex";
import { UserControllerService } from "../../api";
import ACCESS_ENUM from "@/access/accessEnum";
import message from "@arco-design/web-vue/es/message";

// getters
const getters = {};

export default {
  namespaced: true,
  state: {
    loginUser: {
      userName: "未登录",
    },
  },
  getters,
  actions: {
    async getLoginUser({ commit, state }, payload) {
      try {
        //从远程请求登录用户信息
        const res = await UserControllerService.getLoginUserUsingGet();
        if (res.code === 0) {
          commit("updateUser", res.data);
        } else {
          commit("updateUser", {
            ...state.loginUser,
            userRole: ACCESS_ENUM.NOT_LOGIN,
          });
        }
      } catch (error) {
        message.error("当前网络不好,请检查网络配置");
      }
    },
    async setLoginUser({ commit }, payload) {
      commit("updateUser", payload);
    },
    async logout({ commit }) {
      await UserControllerService.userLogoutUsingPost();
      commit("updateUser", {
        userName: "未登录",
        userRole: ACCESS_ENUM.NOT_LOGIN,
      });
    },
  },
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
